package cn.gcyl.module.biz.controller.admin.columntype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

import cn.gcyl.framework.excel.core.util.ExcelUtils;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gcyl.module.biz.controller.admin.columntype.vo.*;
import cn.gcyl.module.biz.dal.dataobject.columntype.ColumnTypeDO;
import cn.gcyl.module.biz.service.columntype.ColumnTypeService;

@Tag(name = "管理后台 - 栏目类型")
@RestController
@RequestMapping("/biz/column-type")
@Validated
public class ColumnTypeController {

    @Resource
    private ColumnTypeService columnTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建栏目类型")
//    @PreAuthorize("@ss.hasPermission('biz:column-type:create')")
    public CommonResult<Integer> createColumnType(@Valid @RequestBody ColumnTypeSaveReqVO createReqVO) {
        return success(columnTypeService.createColumnType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新栏目类型")
//    @PreAuthorize("@ss.hasPermission('biz:column-type:update')")
    public CommonResult<Boolean> updateColumnType(@Valid @RequestBody ColumnTypeSaveReqVO updateReqVO) {
        columnTypeService.updateColumnType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除栏目类型")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:column-type:delete')")
    public CommonResult<Boolean> deleteColumnType(@RequestParam("id") Integer id) {
        columnTypeService.deleteColumnType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除栏目类型")
//                @PreAuthorize("@ss.hasPermission('biz:column-type:delete')")
    public CommonResult<Boolean> deleteColumnTypeList(@RequestParam("ids") List<Integer> ids) {
        columnTypeService.deleteColumnTypeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得栏目类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('biz:column-type:query')")
    public CommonResult<ColumnTypeRespVO> getColumnType(@RequestParam("id") Integer id) {
        ColumnTypeDO columnType = columnTypeService.getColumnType(id);
        return success(BeanUtils.toBean(columnType, ColumnTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得栏目类型分页")
//    @PreAuthorize("@ss.hasPermission('biz:column-type:query')")
    public CommonResult<PageResult<ColumnTypeRespVO>> getColumnTypePage(@Valid ColumnTypePageReqVO pageReqVO) {
        PageResult<ColumnTypeDO> pageResult = columnTypeService.getColumnTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ColumnTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出栏目类型 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:column-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportColumnTypeExcel(@Valid ColumnTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ColumnTypeDO> list = columnTypeService.getColumnTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "栏目类型.xls", "数据", ColumnTypeRespVO.class,
                        BeanUtils.toBean(list, ColumnTypeRespVO.class));
    }

}